package com.netcracker.studportal.server.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.json.simple.JSONObject;

@Entity
@Table(name = "Users")
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_seq_gen")
    @SequenceGenerator(name = "my_seq_gen", sequenceName = "seq_user")
	@Column(name= "user_id")
	private Long user_id;
	
	@Column(name= "type")
	private String type;
	
	@Column(name= "login")
	private String login;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id")
	private Student stud;
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id")
	private Professor prof;
	
	//getters
	
	public Long getId(){return this.user_id;}
	
	public String getType(){return this.type;}
	
	public String getLogin(){return this.login;}
	
	public Student getStudent(){return this.stud;}
	
	public Professor getProfessor(){return this.prof;}
	
	//setters
	
	public void setId(Long id){this.user_id=id;}
	
	public void setType(String type){this.type=type;}
	
	public void setLogin(String login){this.login=login;}
	
	public void setStudent(Student stud){this.stud=stud;}
	
	public void setProfessor(Professor prof){this.prof=prof;}
	
	public JSONObject kJSON(){
		JSONObject obj=new JSONObject();
		if(this.user_id!=null) obj.put("user_id", this.user_id.toString());
		obj.put("type", this.type);
		obj.put("login", this.login);
		
		if(this.stud!=null){
			obj.put("stud_first_name", this.stud.getFirst_name());	
			obj.put("stud_last_name", this.stud.getLast_name());
		}
		
		if(this.prof!=null){
			obj.put("prof_first_name", this.prof.getFirst_name());	
			obj.put("patronymic", this.prof.getPatronymic());
			obj.put("prof_last_name", this.prof.getLast_name());
		}
		
		return obj;
	}
}
